// pages/movies/movie-more/movie-more.js
var app=getApp();
var util=require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEmpty:true,
    totalCount:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var category = options.category;
      //将获取到的标题设置给data进行全局配置
      this.setData({
          category:category
      })
      var dataUrl="";
      switch(category){
          case "正在上映的电影-北京":
              //获取热映电影(默认取20条)
              dataUrl = "/v2/movie/in_theaters";
          break;
          case "即将上映的电影":
              //获取最新电影(默认取20条)
              dataUrl = "/v2/movie/coming_soon";
          break;
          case "豆瓣电影Top250":
              //获取TOP250电影(默认取20条)
              dataUrl = "/v2/movie/top250";
          break;
      }
      this.setData({
          requestUrl: dataUrl
      })
      var that=this;
      wx.request({
          url: app.globalData.doubanBase+dataUrl,
          method: 'GET',
          header: { 'Content-type': 'json' },
          success: function (res) {
              that.processDoubanData(res.data);
          },
          fail: function () {
              console.log("获取电影数据失败");
          },
          complete: function () { }
      })
  },
  processDoubanData: function (moviesDouban) {
      var movies = [];
      for (var subject of moviesDouban.subjects) {
          var title = subject.title;
          if (title.length >= 6) {
              title = title.substring(0, 6) + "...";
          }
          var temp = {
              stars: util.convertToStarsArray(subject.rating.stars),
              title: title,
              average: subject.rating.average,
              coverageUrl: subject.images.large,
              movieId: subject.id
          };
          movies.push(temp);
      }
      
      var totalMovies={};
      if(!this.data.isEmpty){
          totalMovies=this.data.movies.concat(movies);
      }else{
          totalMovies=movies;
          this.setData({
              isEmpty:false
          })
      }
      var result = { movies: totalMovies};
      this.setData(result);
      this.setData({ totalCount: this.data.totalCount + 20 });
      wx.hideNavigationBarLoading();//隐藏标题上的loading动画
  },
  onReachBottom:function(event){
      console.log("reachBottom");
      //页面拉到最下面的事件
      var queryUrl = this.data.requestUrl + "?start=" + this.data.totalCount + "&count=20";
      var that = this;
      wx.request({
          url: app.globalData.doubanBase + queryUrl,
          method: 'GET',
          header: { 'Content-type': 'json' },
          success: function (res) {
              that.processDoubanData(res.data);
          },
          fail: function () {
              console.log("获取电影数据失败");
          },
          complete: function () { }
      })
      wx.showNavigationBarLoading();//在标题上显示loading动画
  },
  ontapDetail: function (event) {
    var movieId = event.currentTarget.dataset.movieid;
    wx.navigateTo({
      url: '../movie-detail/movie-detail?movieId=' + movieId,
    })
  },
  onPullDownRefresh:function(event){
        var that=this;
        this.data.movies={}
        this.setData({isEmpty:true,totalCount:0});
        var refreshUrl=this.data.requestUrl+"?start=0&count=0";
        wx.request({
            url: app.globalData.doubanBase + refreshUrl,
            method: 'GET',
            header: { 'Content-type': 'json' },
            success: function (res) {
                that.processDoubanData(res.data);
            },
            fail: function () {
                console.log("获取电影数据失败");
            },
            complete: function () { }
        })
        wx.showNavigationBarLoading();
  },
  onReady:function(event){
      //动态设置页面导航条标题(不能放在onLoad中)
      wx.setNavigationBarTitle({
          title: this.data.category
      })
  }
})